﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dss.Data;

namespace Dss.Methods
{
    public class NewProject
    {
        public NewProject() {
            cecbContext = new DSSEntities();
        }
        //crete DB objects
        DSSEntities cecbContext;
        Project prj;

        //project insert method
        
        public bool InsertProject(String name, DateTime sDate, DateTime eDate, String cat,  String teamref)
        {
            bool IsInserted = false;
            try
            {
                prj = new Project();
                String reference = getNewProjectReference();
                //call filde throuh the object
                prj.Project_Reference = reference;
                prj.Project_Name = name;
                prj.Project_StartDate = sDate;
                prj.Project_EndDate = eDate;
                prj.Project_Catagory = cat;
                prj.Project_TeamReference = teamref;
                prj.Project_Status = true;
                cecbContext.AddToProjects(prj);
                cecbContext.SaveChanges();
                IsInserted = true;
            }
            catch (Exception )
            {
                IsInserted = false;
            }


            return IsInserted;
        }


        //Generate a Reference for new define Project
        public String getNewProjectReference()
        {

            var idKey = (from c in cecbContext.Projects
                         orderby c.ProjectID descending
                         select c).First();

            String id = idKey.Project_Reference.ToString();
            int sub = int.Parse(id.Substring(3)) + 1;
            String newId = "Pro" + sub;
            return newId.ToString();
        }

    }
}
